package models

import (
	"fmt"
	"github.com/syyongx/php2go"
	"hello/wl"
)

type User struct {
	Id   int
	Username string
	Email string
	Tou string
	Password string
	Salt string
	Access_token string
	Usertype int
	BaseMode
}
type UserName struct {
	Id   int
	Username string
}
type UserShow struct {
	Id   int
	Username string
	Email string
	Tou string
	BaseMode
}
//粉丝
type Fan struct {
	Id int
	FanId int
	StarId int
	BaseMode
}

//根据邮箱查找
func (this *User) FindByEmail(){

	wl.DB.Where("email = ?",this.Email).Find(&this)
}

//根据github查找
func (this *User) FindByNode(){
	if  !php2go.Empty(this.Password){
		wl.DB.Where("password = ?",this.Password).Find(&this)
	}

}

//根据ID查找
func (this *UserShow) FindById(){

	wl.DB.Find(&this)

}

//根据ID 查找粉丝数和关注数
func (this *User) FanStarCount(fanCount,starCount *int){

	//关注的数量
	wl.DB.Table("fans").Where("fan_id= ?",this.Id).Count(starCount)
	//被关注的数量
	wl.DB.Table("fans").Where("star_id= ?",this.Id).Count(fanCount)

}

//根据ID 查找粉丝数用户
func (this *User) FanUsers(fanUsers *[]UserShow,page,limit int){

	//粉丝的数量
	wl.DB.Table("fans").
		Select("user.*").
		Joins(" LEFT JOIN user ON user.`id`=fans.`star_id` ").
		Where("fan_id= ?",this.Id).
		Limit(limit).Offset((page-1)*limit).
		Find(fanUsers)

}

//根据ID 查找关注数用户
func (this *User) StarUsers(starUsers *[]UserShow,page,limit int){

	//粉丝的数量
	wl.DB.Table("fans").
		Select("user.*").
		Joins(" LEFT JOIN user ON user.`id`=fans.`fan_id` ").
		Where("star_id= ?",this.Id).
		Limit(limit).Offset((page-1)*limit).
		Find(starUsers)

}

//创建用户
func (this *User) CreateUser(){
	wl.DB.Create(&this)

}
//修改AccessToken
func (this *User) ReloadAccessToken() bool{

	access_token:=php2go.Md5(this.Username+php2go.Uniqid(""))

	rows:=wl.DB.Model(&this).Updates(map[string]interface{}{"access_token": access_token}).RowsAffected

	fmt.Println(rows)
	if php2go.Empty(rows) {
		return false
	}else{
		return true
	}
}

//修改用户信息
func (this *User) UpdateInfo() bool{

	rows:=wl.DB.Model(&this).Updates(map[string]interface{}{"username": this.Username,"tou":this.Tou}).RowsAffected

	fmt.Println(rows)
	if php2go.Empty(rows) {
		return false
	}else{
		return true
	}
}

//用户的所有文章
func (this *User) UserPosts(page int,limit int) ([]Post,int) {
	posts:=[]Post{}
	var count int
	if  !php2go.Empty(this.Id){
		wl.DB.Select("id,title,created_at").
			Where("user_id = ?",this.Id).
			Limit(limit).Offset((page-1)*limit).
			Order("created_at desc").
			Find(&posts)

		wl.DB.Table("post").Where("user_id = ?",this.Id).Count(&count)

	}
	return posts,count
}

//用户赞过的所有文章
func (this *User) UserZanPosts(page int,limit int) ([]Post,int) {
	posts:=[]Post{}
	var count int
	if  !php2go.Empty(this.Id){
		//表里有不存的文章ID
		wl.DB.Table("zan").
			Select("post.id,post.title,zan.created_at").
			Joins("inner JOIN post ON zan.`post_id`=post.`id`").
			Where("zan.user_id = ?",this.Id).
			Limit(limit).Offset((page-1)*limit).
			Order("created_at desc").
			Find(&posts)


		wl.DB.Table("zan").
			Joins("inner JOIN post ON zan.`post_id`=post.`id`").
			Where("zan.user_id = ?",this.Id).
			Count(&count)

	}
	return posts,count
}



//用户评论的所有文章
func (this *User) UserCommentPosts(page int,limit int) ([]Post,int) {
	posts:=[]Post{}
	var total ExecTotal
	if  !php2go.Empty(this.Id){
		//表里有不存的文章ID
		wl.DB.Table("comment").
			Select("post.id,post.title,comment.created_at").
			Joins("inner JOIN post ON comment.`post_id`=post.`id`").
			Where("comment.user_id = ?",this.Id).
			Limit(limit).Offset((page-1)*limit).
			Order("created_at desc").
			Group("post.`id`").
			Find(&posts)


		wl.DB.Raw("SELECT COUNT(*)  AS total  FROM(SELECT COUNT(*) FROM  comment " +
			"INNER JOIN post ON comment.`post_id`=post.`id` " +
			"WHERE comment.`user_id`=? " +
			"GROUP BY post.`id`) as count ", this.Id).Scan(&total)


	}
	return posts,total.Total
}

//查询是否关注
func (this *Fan)IsFan()   {

	wl.DB.Where("fan_id = ? and star_id=?", this.FanId,this.StarId).Find(&this)

}
//添加赞
func (this *Fan)CreatedFan()   {

	wl.DB.Create(&this)

}

//取消赞
func (this *Fan)DeleteFan()  int64 {

	return wl.DB.Delete(&this).RowsAffected

}



func (u *UserName) TableName() string {
	return "user"
}
func (u *User) TableName() string {
	return "user"
}
func (u *UserShow) TableName() string {
	return "user"
}
func (u *Fan) TableName() string {
	return "fans"
}

